package wint.lang.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import wint.lang.io.FastByteArrayOutputStream;

public class FileUtil {
	
	public static String getFileExtension(String name) {
		if (StringUtil.isEmpty(name)) {
			return StringUtil.EMPTY;
		}
		int pos = name.lastIndexOf('.');
		if (pos < 0) {
			return StringUtil.EMPTY;
		}
		return name.substring(pos + 1);
	}

	public static byte[] getFileContent(File file) throws IOException {
		FileInputStream is = new FileInputStream(file);
		FastByteArrayOutputStream bos = new FastByteArrayOutputStream();
		IoUtil.copy(is, bos);
		IoUtil.close(is);
		IoUtil.close(bos);
		return bos.toByteArray();
	}
	
	public static String getFileContentString(File file, String charset) throws UnsupportedEncodingException, IOException {
		return new String(getFileContent(file), charset);
	}
	
}
